package com.mawujun.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener{
	private String id;
	
	private String name;
	private String password;
	private String zhangt;
	private Date date;
	
	public int getYear() {
		if(date==null){
			date=new Date();
		}
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}
	
	public String getDbName() {
		return "UFDATA_"+zhangt+"_"+getYear();
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getZhangt() {
		return zhangt;
	}
	public void setZhangt(String zhangt) {
		this.zhangt = zhangt;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	

	public void valueBound(HttpSessionBindingEvent event) {
		HttpSession session = event.getSession();
		ServletContext ctx = session.getServletContext();
		//User user=(User)session.getAttribute("user");
		//if(user!=null) {
			HashMap<String,Integer>  onLineUser = (HashMap<String,Integer>)ctx.getAttribute("onLineUser");
			if (onLineUser == null) { // 如果是服务器启动后的第一个登录用户，新建一个容器
				onLineUser = new HashMap<String,Integer>();
				onLineUser.put(this.getId(), 1); // 把自己加入容器中
				ctx.setAttribute("onLineUser", onLineUser);
			}
		//}
		
	}

	public void valueUnbound(HttpSessionBindingEvent event) {
		HttpSession session = event.getSession();
		ServletContext ctx = session.getServletContext();
		HashMap<String,Integer>  onLineUser = (HashMap<String,Integer>)ctx.getAttribute("onLineUser");
		if (onLineUser != null) { // 如果是服务器启动后的第一个登录用户，新建一个容器
			onLineUser.remove(this.getId());
		}
	}
}
